package com.fenqing.leetcode.code.t0_200.t0_100;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.*;

/**
 * @author fenqing
 */
public class Topic049 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic049(String input) {
        super(input);
    }

    @Execute
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, Integer> hash = new HashMap<>();
        List<List<String>> result = new ArrayList<>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String newStr = new String(chars);
            Integer integer = hash.get(newStr);
            if (integer == null) {
                List<String> list = new ArrayList<>();
                list.add(str);
                result.add(list);
                hash.put(newStr, result.size() - 1);
            } else {
                result.get(integer).add(str);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Topic049 topic049 = new Topic049(
                """
                        ["eat", "tea", "tan", "ate", "nat", "bat"]
                        """
        );
        List<List<String>> run = topic049.run();
        System.out.println(run);
    }
}
